package com.zzptc.myapp1204;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.zzptc.myapp1204.Service.CarInfoService;
import com.zzptc.myapp1204.adapter.QcGzAdapter;
import com.zzptc.myapp1204.entity.CarInfo;
import com.zzptc.myapp1204.entity.QcGzInfo;

import org.litepal.LitePal;
import org.w3c.dom.Text;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    ListView lv_qcgz;
    TextView textView;
    ListView<QcGzInfo> qcGzInfoInfos=new ArrayList<>();

    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.tv);

        //getCarInfo();

        getQcGzInfo();
        initView();

    }

    private void initView() {
        lv_qcgz=findViewById(R.id.lv_qcgz);
        qcGzInfoInfos= LitePal.findAll(QcGzInfo.class);
        adapter=new QcGzAdapter(R.layout.qcgz_item.,qcGzInfoInfos) {
            lv_qcgz.setAdapter(adapter);
        }


    }

    private void getQcGzInfo() {
        Retrofit r=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        CarInfoService carInfoService = r.create(CarInfoService.class);
        Call<QcGzInfo> qcGzAll = carInfoService.getQcGzAll
                ("6b40135084049a9641f5dc01af7ed188", "P2079");

        qcGzAll.enqueue(new Callback<QcGzInfo>() {
            @Override
            public void onResponse(Call<QcGzInfo> call, Response<QcGzInfo> response) {
                QcGzInfo body = response.body();


                resultDTO.save();

                Log.d(TAG, "onResponse: "+resultDTO.getwhy());



            }

            @Override
            public void onFailure(Call<QcGzInfo> call, Throwable t) {

            }
        });


    }

    private void getCarInfo() {
        Retrofit r=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        CarInfoService carInfoService = r.create(CarInfoService.class);

        Call<CarInfo>all=carInfoService.getAll();
        all.enqueue(new Callback<CarInfo>() {
            @Override
            public void onResponse(Call<CarInfo> call, Response<CarInfo> response) {
                CarInfo ca=response.body();
                String  s=ca.getData().get(0).getWho();
                textView.setText(s);

            }

            @Override
            public void onFailure(Call<CarInfo> call, Throwable t) {

            }
        });

    }
}
